package com.jhf.skypac_pda.db.impl;

import android.content.Context;

import com.jhf.skypac_pda.HxApplication;
import com.jhf.skypac_pda.db.DBManager;
import com.jhf.skypac_pda.db.bean.DB_Permission;
import com.jhf.skypac_pda.db.bean.DB_Permission;
import com.jhf.skypac_pda.db.gen.DB_PermissionDao;
import com.jhf.skypac_pda.db.gen.DB_PermissionDao;
import com.jhf.skypac_pda.db.gen.DaoSession;
import com.jhf.skypac_pda.utils.PrefUtils;
import com.jhf.skypac_pda.utils.TimeUtils;

import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.Date;

public class PermissionDaoImp {
    private static PermissionDaoImp instance;
    private DB_PermissionDao dao;


    private PermissionDaoImp(Context context) {
        DaoSession daoSession = DBManager.getInstance(context).getDaoSession(context);
        dao = daoSession.getDB_PermissionDao();
    }

    public static synchronized PermissionDaoImp getInstance(Context context) {
        if (instance == null)
            instance = new PermissionDaoImp(context);
        return instance;
    }

    public static void init() {
        instance = null;
    }


    public void savePermissions(ArrayList<String> mPermissionList) {
        for (String permission :
                mPermissionList) {
            if (permission.contains("pda:")) {
                DB_Permission bean = new DB_Permission();
                bean.setModulePermission("pda/materialtank");
                bean.setModuleName("料罐投料");
                bean.setModuleFuctionPermission(permission);
                dao.insert(bean);
            }
        }
    }

    public boolean isHavePermission(String permission) {
        //TODO:暂时去掉权限控制
        //return true;

        //是管理员，单独提前处理
        if (PrefUtils.getBoolean(HxApplication.getIntance().getApplicationContext(), "isAdmin", false)) {
            return true;
        }

        QueryBuilder<DB_Permission> queryBuilder = dao.queryBuilder();
        Query<DB_Permission> query = queryBuilder.where(DB_PermissionDao.Properties.ModuleFuctionPermission.eq(permission))
                .build();
        ArrayList<DB_Permission> infos = (ArrayList)query.list();
        if (infos == null || infos.size() == 0) return false;
        return true;
    }

    public void deleteAll() {
        dao.deleteAll();
    }
}
